<template>
  <!-- 初步诊断 -->
  <section>
    <el-select
      :value="value"
      value-key="firstVist"
      placeholder="请选择"
      allow-create
      default-first-option
      filterable
      remote
      clearable
      :remote-method="remoteMethod"
      @change="onChange"
      @clear="remoteMethod"
      style="width:100%"
    >
      <el-option v-for="(item,index) in array" :key="index" :label="item.firstVist" :value="item"></el-option>
      <br />
      <pager v-model="pager" small @change-pagenum="getData"></pager>
    </el-select>
  </section>
</template>
<script>
export default {
  props: {
    value: {
      type: Object,
      default: function () {
        return null;
      },
    },
    deptId: [Number, String],
    doctorId: [Number, String],
  },
  components: {},
  data() {
    return {
      array: [],
      form: {
        firstVisit: "",
      },
      pager: { pageSize: 5, pageNum: 1, total: 0 },
    };
  },
  mounted() {
    let th = this;
    //  console.log(this.doctorId);
    th.getData();
  },
  methods: {
    remoteMethod(name) {
      let th = this;
      th.form.firstVisit = name;
      th.pager.pageNum = 1;
      th.getData();
    },
    getData() {
      let th = this;
      let j = {
        // deptId: th.deptId
        deptId:''
      };
      Object.assign(j, th.pager, th.form);
      th.$api.getCureFirstVisitRecord(j).then((res) => {
        th.array = res.dataBody.list;
        th.pager.total = res.dataBody.total;
      });
    },
    onChange(value) {
      let th = this;
      if (!value) return;
      //判断是否应该添加
      if (typeof value != "string") {
        th.$emit("input", value);
        th.$emit("success", value);
      } else {
        let j = {
          hpId: th.adminUser.hpId,
          deptId: th.deptId,
          doctorId: th.doctorId,
          firstVist: value,
          id: "",
        };
        th.$api.addCureFirstVisitRecord(j).then((res) => {
          if (res.executed) {
            j.id = res.data;
            th.$emit("input", j);
            th.$emit("success", j);
            th.remoteMethod();
          }
        });
      }
    },
  },
};
</script>